<template>
  <view>
    <!-- 搜索框 -->
    <my-search @clickXxx="gotoSearch" :radius="18" ></my-search>
    <!-- 分类导航 -->
    <view class="cate-container">
      <!-- 左边一级分类 -->
        <scroll-view class="cate-left" scroll-y="true" :style="{height: wh+'px'}">
          <view
            :class="['cate-left-item', active===i ? 'active':'' ] "
            v-for="(item, i) in cateList"
            :key="i"
            @click="changeActive(i)"
          >
            {{item.cat_name}}
          </view>

        </scroll-view>
      <!-- 右边一级分类详情 -->
      <scroll-view class="cate-right" scroll-y="true" :style="{height: wh+'px'}" :scroll-top="scrollTop">
        <!-- 右边一级分类详情item -->
        <view class="cate-right-item" v-for="(item, i) in cateLevl2" :key="i">
          <!-- 分类详情title -->
          <view class="cate-right-item-title">
            /{{item.cat_name}}/
          </view>
          <!-- 分类详情列表 -->
          <view class="cate-right-item-content">
            
            <view class="cate-right-item-content-item" v-for="(item2, i2) in item.children" :key="i2" @click="gotoGoodsList(item2)">
              <image :src="item2.cat_icon" mode="widthFix"></image>
              <text>{{item2.cat_name}}</text>
            </view>
            
          </view>
        </view>
        
      </scroll-view>
    </view>
  </view>
</template>

<script>
  import badgeMix from '@/mixins/tabbar-badge.js'
  
  export default {
    mixins: [badgeMix], 
    data() {
      return {
        // UI(页面)状态
        wh: 0, // 屏幕可用高度
        active: 0, // 选中的一级分类
        scrollTop: 0, //滚动条距离顶部的距离
        // 数据状态
        cateList: [], // 分类数据
        cateLevl2: [], // 分类详情
      };
    },
    onLoad() {
      // 获取系统信息
      const sysInfo = uni.getSystemInfoSync()
      // 赋值数据到wh
      this.wh = sysInfo.windowHeight - 50 // 减去自定义搜索栏的高度
      
      // 发起数据请求
      this.getCateList()
    },
    methods: {
      // 获取分类数据
      async getCateList() {
        const { data: res } = await uni.$http.get('/categories')
        console.log(res)
        // 异常数据判断
        if(res.meta.status !== 200) return uni.$showMsg()
        // 赋值数据到data中
        this.cateList = res.message
        // 赋值数据到二级分类
        this.cateLevl2 = res.message[0].children
      },
      // 改变一级导航选中索引
      changeActive(i){
        this.active = i
        // 重新给二级导航赋值
        this.cateLevl2 = this.cateList[i].children
        // 滚动条位置设置到顶部
        this.scrollTop = Math.random()
      },
      // 跳转到商品列表页
      gotoGoodsList(item) {
        console.log(item)
        uni.navigateTo({
          url: `/subpkg/goods_list/goods_list?cid=${item.cat_id}`
        })
      },
      gotoSearch(){
        // 跳转到搜索页
        console.log('父组件 goto search页面')
        uni.navigateTo({
          url: '/subpkg/search/search'
        })
      }
    },

  }
</script>

<style lang="scss">
  .cate-container {
    display: flex;
    .cate-left {
      height: 500rpx;
      width: 232rpx;
      .cate-left-item {
        display: flex;
        justify-content: center;
        align-items: center;
        height: 110rpx;
        background-color: #efefef;
        font-size: 24rpx;
        &.active {
          background-color: #ffffff;
          display: flex;
          justify-content: space-between;
          &::before {
            content: '';
            width: 4rpx;
            height: 55rpx;
            background-color: #c00000;
          }
          &::after {
            content: '';
            width: 4rpx;
            height: 55rpx;
            background-color: #ffffff;
          }
        }
      }
    }
    .cate-right {
      .cate-right-item {
        .cate-right-item-title{
          display: flex;
          justify-content: center;
          align-items: center;
          height: 60rpx;
          font-weight: bold;
        }
        .cate-right-item-content {
          display: flex;
          flex-wrap: wrap;
          .cate-right-item-content-item {
            width: 33.33%;
            padding: 16rpx;
            display: flex;
            flex-direction: column;
            justify-content: center;
            align-items: center;
            box-sizing: border-box;
            image {
              width: 100%;
            }
            text {
              font-size: 24rpx;
            }
          }
        }
      }
    }
  }
</style>
